﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrameWork.Lib;

namespace REVISIONMAX30
{
    public partial class frmSQLItem : Form
    {
        public frmSQLItem()
        {
            InitializeComponent();
        }

        public UIManager UIManager { get; set; }
        public DataObjects.SQLItem SQLItem { get; set; }
        public DataObjects.Group Group { get; set; }

        public void View(DataObjects.SQLItem pChildObject, DataObjects.Group pParentObject)
        {

            this.SQLItem = pChildObject;
            this.Group = pParentObject;
            this.UpdateViewer();
            base.ShowDialog();
        }

        public Guid DataObjectGUID
        {
            get { return this.SQLItem.DataObjectGUID; }
        }

        private void UpdateObject()
        {
            this.SQLItem.ExecutionOrderIndex = this.txtIndex.Text.IntValueOrZero();
            this.SQLItem.Revision = this.txtRevision.Text.IntValueOrZero();
            this.SQLItem.SourceType = this.cboSourceType.SelectedValue.StringValueOrEmpty();

            if (this.SQLItem.SourceType == "SQLFILE")
            {
                this.SQLItem.Source = this.txtFileName.Text;
                this.SQLItem.SetSourceName();
            }
            else if (this.SQLItem.SourceType == "COMMANDTEXT")
            {
                this.SQLItem.CommandText = this.txtCommandText.Text;
            }

            if (this.cboConnectionStrings.SelectedValue != null)
            {
                this.SQLItem.SelectedConnectionStringGUID = (Guid)this.cboConnectionStrings.SelectedValue;
                var tConnectionString = this.Group.ConnectionStringList.Where(t => t.DataObjectGUID == this.SQLItem.SelectedConnectionStringGUID).FirstOrDefault();
                if (tConnectionString != null)
                {
                    this.SQLItem.SelectedConnectionStringName = tConnectionString.Name;
                }
                else
                {
                    this.SQLItem.SelectedConnectionStringName = "";
                }

            }
            else
            {
                this.SQLItem.SelectedConnectionStringGUID = null;
                this.SQLItem.SelectedConnectionStringName = "";
            }
        }

        private void UpdateViewer()
        {
            this.btnExpire.Text = !this.SQLItem.Expired.HasValue ? "Expire" : "Un-Expire";
            this.txtExecuted.Text = this.SQLItem.Executed.StringValueOrEmpty();
            this.txtExpired.Text = this.SQLItem.Expired.StringValueOrEmpty();
            this.txtIndex.Text = this.SQLItem.ExecutionOrderIndex.ToString();
            this.txtRevision.Text = this.SQLItem.Revision.StringValueOrEmpty();
            if (this.SQLItem.SourceType == "SQLFILE")
            {
                this.txtFileName.Text = this.SQLItem.Source;
            }
            else if (this.SQLItem.SourceType == "COMMANDTEXT")
            {
                this.txtCommandText.Text = this.SQLItem.CommandText;
            }

            this.cboSourceType.DataSource = Types.SourceTypeList;
            this.cboSourceType.DisplayMember = "Value";
            this.cboSourceType.ValueMember = "Key";
            this.cboSourceType.SelectedValue = this.SQLItem.SourceType.StringValueOrEmpty();

            this.cboConnectionStrings.DataSource = this.Group.ConnectionStringList.Select(t => new { Key = t.DataObjectGUID, Value = t.Name }).ToList();
            this.cboConnectionStrings.DisplayMember = "Value";
            this.cboConnectionStrings.ValueMember = "Key";
            if (this.SQLItem.SelectedConnectionStringGUID != null)
                this.cboConnectionStrings.SelectedValue = this.SQLItem.SelectedConnectionStringGUID;
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        public void Save(bool pApply)
        {

            UpdateObject();

            if (!Group.SQLItemList.Exists(t => t.DataObjectGUID == this.DataObjectGUID))
            {
                Group.SQLItemList.Add(this.SQLItem);
            }
            else
            {
                this.SQLItem.Updated = DateTime.Now;
                this.Group.SQLItemList.RemoveAll(t => t.DataObjectGUID == this.SQLItem.DataObjectGUID);
                this.Group.SQLItemList.Add(this.SQLItem);
            }


            if (!pApply)
            {
                base.Close();
            }
            else
            {
                UpdateViewer();
            }

        }

        private void btnExpire_Click(object sender, EventArgs e)
        {
            if (this.SQLItem.Expired != null)
                this.SQLItem.Expired = DateTime.Now;
            else
                this.SQLItem.Expired = null;

            this.btnExpire.Text = !this.SQLItem.Expired.HasValue ? "Expire" : "Un-Expire";

            Save(false);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);

            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtFileName.Text = this.openFileDialog1.FileName;
            }
        }

        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
            txtFileName.ForeColor = System.IO.File.Exists(txtFileName.Text) ? Color.Black : Color.Red;
        }

    }
}
